<?php
$isReloadable = false;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');
$aFlagUte = array();
require "_inc/inc.aFlagUte.php";

//Gestione azioni
if (reqvar('azione')=='salva'){
	$msgPag .= salva($aFlagUte);
}

$sSql = "SELECT * FROM utente WHERE id_utente = ".reqVarInt('id');
$objDB->exec($sSql);
$aRec = $objDB->fetch();
if (!$aRec) {
	backOnErr("id '".reqVar('id')."' inesistente !");
	exit;
}
reqDef('login',$aRec['login']);
reqDef('nome',$aRec['nome']);
reqDef('e_mail',$aRec['e_mail']);
reqDef('ric_ope',$aRec['ric_ope']);
reqDef('password',$aRec['password']);
foreach($aFlagUte as $sNC => $aFU) {
	reqDef($sNC,$aRec[$sNC]);
}

$sCmdL = "";
$sCmdR = "";

if ($objSess->abilitaz['gescomm'] == 'W') {
	$sCmdR .= "<a class='cmd1' href='javascript:document.frm1.submit();' >salva</a>";
}	
$sCmdR .= cmdTorna('torna');

$aMenuPag = array();
include("_inc/inc.aMenuPag.php");
testaHtml('Modifica Utente', $msgPag, '30.10.10', $aMenuPag);
echo getHtmlForm();
echo getHtmlHidden('id');
echo getHtmlHidden('xsn');
echo getHtmlHidden('azione','salva');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
      <td class="cmd1" align="right"><?=$sCmdR?></td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php echo getHtmlRigaRif();?>
	<tr>
		<td class="tit1" colspan='20' align='center'>Utente</td>
	</tr>
    <tr>
   	<td class="lab1" colspan='2'>Login</td>
	  	<td class="txt1" colspan='6'><?echo getHtmlText('login',30);?></td>
   	<td class="lab1" colspan='2'>Nome</td>
	  	<td class="txt1" colspan='10'><?echo getHtmlText('nome', 60);?></td>
    </tr>
    <tr>
   	<td class="lab1" colspan='2'>Password</td>
	  	<td class="txt1" colspan='6'><?echo getHtmlText('password',30);?></td>
   	<td class="lab1" colspan='2'>E-mail</td>
	  	<td class="txt1" colspan='10'><?echo getHtmlText('e_mail', 60);?></td>
    </tr>
<?php 
foreach($aFlagUte as $sNC => $aFU) {
?>
    <tr>
   	<td class="lab1" colspan='2'><?php echo $sNC;?></td>
	  	<td class="txt1" colspan='3'><?echo getHtmlSelect($sNC,$aFU['aVal']);?></td>
	  	<td class="txt1" colspan='15'><?echo $aFU['nota'];?></td>
    </tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
      <td class="cmd1" align="right"><?=$sCmdR?></td>
   </tr>
</table>
<?
codaHtml();
exit();

function salva($aFlagUte) {
	global $objDB, $objSess;

	$msg = '';
	$_REQUEST['login'] = strtolower(reqVarTrim('login'));
	if (reqVarTrim('login')=='') {
		$msg .= "manca login <br>";
	}
	if (preg_match("/[^0-9a-z_\\-@\\/\\.]/", reqVarTrim('login'))) {
		$msg .= "login : solo lettere numeri _-.@/ <br>";
	}
	$sSql = "SELECT * FROM utente WHERE login = '".reqVarAS('login')."' AND id_utente <> ".reqVarInt('id');
	$objDB->exec($sSql);
	$aRec = $objDB->fetch();
	if ($aRec) {
		$msg .= "login esitente<br>";
	}
	if (reqVarTrim('login')=='') {
		$msg .= "manca login <br>";
	}
	if (reqVarTrim('nome')=='') {
		$msg .= "manca nome <br>";
	}
	if (reqVarTrim('password')=='') {
		$msg .= "manca password <br>";
	}
	
	if ($msg != '') {return $msg;}
	
	$sSql = "UPDATE utente SET login = '".reqvarAS('login')."' "
		.", nome = '".reqvarAS('nome')."' "
		.", e_mail = '".reqvarAS('e_mail')."' "
		.", password = '".reqvarAS('password')."' ";
	foreach($aFlagUte as $sNC => $aFU) {
		$sSql .= ", $sNC = '".reqvarAS($sNC)."' ";
	}
	$sSql .= " WHERE id_utente = ".reqVarInt('id');
	$objDB->query($sSql);
	header("Location: ".urlTorna());
	exit();
}

?>